<template>
  <div class="login-container">
    <el-form
      ref="loginFormRef"
      :model="state.loginForm"
      :rules="state.loginRules"
      class="login-form"
      auto-complete="on"
      :status-icon="false"
      label-position="left"
    >
      <div class="title-box">
        登录
      </div>
      <div class="login-form-options">
        <el-form-item prop="email" class="input-wrapper">
          <el-input
            ref="emailRef"
            v-model="state.loginForm.email"
            :placeholder="state.active.email ? '请输入账号/手机号' : '用户名：请输入你的OA邮箱'"
            name="email"
            type="email"
            size="large"
            :input-style="{ height: '45px' }"
            :class="['login-form-input', state.loginForm.email || state.active.email ? 'active' : '']"
            clearable
            :prefix-icon="User"
            @blur="setActive('email', false)"
            @focus="setActive('email', true)"
          />
        </el-form-item>

        <el-form-item prop="password" class="input-wrapper">
          <el-input
            ref="passwordRef"
            v-model="state.loginForm.password"
            type="password"
            :placeholder="state.active.password ? '请输入密码' : '密码：123456'"
            name="password"
            :input-style="{ height: '45px' }"
            :class="['login-form-input', state.loginForm.password || state.active.password ? 'active' : '']"
            clearable
            size="large"
            :prefix-icon="Lock"
            @blur="setActive('password', false)"
            @focus="setActive('password', true)"
          />
        </el-form-item>
        <!-- <div class="registor">
          没有账号？
          <el-link type="primary">
            立即注册
          </el-link>
        </div> -->
        <el-button
          :loading="state.loading"
          type="primary"
          native-type="submit"
          class="btn-submit"
          size="large"
          @click="handleLogin"
        >
          登录
        </el-button>
      </div>
    </el-form>

    <footer class="footer">
      <span class="info">隐私政策</span>
      <span class="info">成都运荔枝科技有限公司版权所有©1997-2023</span>
      <span class="info">IPC备案：蜀ICP备19017829号</span>
    </footer>
  </div>
</template>

<script setup>
import { reactive, toRefs } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { User, Lock } from '@yun-design/icons-vue';
import { setToken } from '@/utils/auth';
import { localStorageSet } from '@/utils/localStorage';
import { USER } from '@/constant/storage';
import { login } from '@/api/user';

const route = useRoute();
const router = useRouter();
const redirect = route.query?.redirect;
const loginFormRef = ref();
const state = reactive({
  active: {
    email: false,
    password: false,
  },
  loginForm: {
    email: '',
    password: '',
  },
  loginRules: {
    email: [
      { required: true, message: '请输入邮箱地址', trigger: 'input' },
      {
        type: 'email',
        message: '请输入正确的邮箱地址',
        trigger: ['blur', 'change'],
      },
    ],
    password: [{ required: true, trigger: 'input', message: '请输入密码' }],
  },
  loading: false,
  redirect,
  emailRef: null,
  passwordRef: null,
});
const setActive = (key, flag) => {
  state.active[key] = flag;
};
const handleLogin = () => {
  state.loading = true;
  loginFormRef.value.validate()
    .then(() => login(state.loginForm))
    .then((res) => {
      const { token } = res;
      setToken(token);
      localStorageSet(USER, res);
      router.replace({ path: state.redirect || '/' });
    })
    .finally(() => {
      state.loading = false;
    });
};
</script>

<style scoped lang="scss">
.login-container {
  position: relative;
  height: 100%;
  background: #fff url("@/assets/img/login-bg.jpg") 0 0 no-repeat;
  background-size: 100% 100%;
}

.title-box {
  display: flex;
  box-sizing: content-box;
  padding: 80px 40px 32px 40px;
  font-size: 32px;
  height: 40px;
  line-height: 40px;
  font-weight: bold;
  color: rgba(0, 0, 0, 0.85);
  .splitDot {
    width: 5px;
    height: 5px;
    border-radius: 2.5px;
    background: #262626;
    margin: 0 12px;
  }
}

.title-logo {
  width: 40px;
  margin-right: 20px;
}

.login-form {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 400px;
  height: 465px;
  background: #ffffff;
  box-shadow: 0px 2px 24px 0px rgba(167, 177, 183, 0.22);
  border-radius: 8px;

  .registor {
    height: 20px;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    color: #969799;
    font-size: 14px;
  }

  &-options {
    margin: 0 45px;

    .input-wrapper {
      margin-bottom: 24px;
      margin-top: 8px;

      &.is-error {
        :deep(.el-input__prefix-inner) {
          color: #f53f3f;
        }
      }

      &:not(.is-error) {
        .login-form-input:not(.active) {
          :deep(.el-input__wrapper) {
            box-shadow: transparent 0px 0px 0px 1px inset;
          }
        }
      }

      :deep(.el-form-item__error) {
        margin-top: 4px;
        font-size: 12px;
        padding: 0;
        line-height: 18px;
      }
    }
  }

  &-input {
    // height: 45px;
    background-color: #f7f8fa;

    &.active {
      background-color: #fff;
    }

    :deep(.el-input__inner) {
      background-color: transparent;
    }

    :deep(.el-input__wrapper) {
      background-color: transparent;
    }

    :deep(input:-webkit-autofill) {
      box-shadow: 0 0 0 1000px #fff inset;
    }

    :deep(input:autofill) {
      box-shadow: 0 0 0 1000px #fff inset;
    }
  }
}

.btn-submit {
  width: 100%;
  font-size: 16px;
  margin-top: 32px;
  height: 45px;
}

.footer {
  position: absolute;
  left: 0;
  bottom: 16px;
  width: 100%;
  height: 20px;
  color: #969799;
  text-align: center;

  .info {
    margin: 0 12px;
  }
}
.logo{
  width: 131px;
  display: block;
  margin: 0 auto;
  margin-top: -56px;
}
</style>
